import { snippets } from "@/lib/generated/snippets";
import { Snippet } from "@/components/code";
import { Callout, Card, Cards, Steps, Tabs } from "nextra/components";
import UniversalTabs from "@/components/UniversalTabs";

# On-Failure Tasks

The on-failure task is a special type of task in Hatchet that allows you to define a function to be executed in the event that any task in the main task fails. This feature enables you to handle errors, perform cleanup tasks, or trigger notifications in case of task failure within a workflow.

## Defining an on-failure task

You can define an on-failure task on your task the same as you'd define any other task:

<UniversalTabs items={["Python", "Typescript", "Go"]}>
  <Tabs.Tab title="Python">
    <Snippet src={snippets.python.on_failure.worker.on_failure_step} />
    <Callout variant="warning">
      Note: Only one on-failure task can be defined per workflow.
    </Callout>
  </Tabs.Tab>
  <Tabs.Tab title="Typescript">
    <Snippet src={snippets.typescript.on_failure.workflow.on_failure_task} />
  </Tabs.Tab>
  <Tabs.Tab title="Go">
    <Snippet src={snippets.go.on_failure.main.on_failure_task} />
  </Tabs.Tab>
</UniversalTabs>

In the examples above, the on-failure task will be executed only if any of the main tasks in the workflow fail.

## Use Cases

Some common use cases for the on-failure task include:

- Performing cleanup tasks after a task failure in a workflow
- Sending notifications or alerts about the failure
- Logging additional information for debugging purposes
- Triggering a compensating action or a fallback task

By utilizing the on-failure task, you can handle workflow failures gracefully and ensure that necessary actions are taken in case of errors.
